package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.OrgUpdateDto;
import com.tcm.entity.vo.OrgResult;
import com.tcm.entity.resp.R;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.service.OrganizationService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/4 19:46
 */
@Slf4j
@RestController
@RequestMapping("/pc/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService orgService;

    /**
     * 获取组织机构节点
     * @param parentId 父节点，为空为顶级节点
     * @return 阻止机构节点
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getOrgNodesByParentId(String parentId){
        List<OrgResult> nodes = orgService.getOrgNodesByParentId(StringUtils.hasText(parentId) ? Long.parseLong(parentId) : null);
        return R.success(nodes);
    }

    /**
     * 添加组织机构
     * @param request http请求对象
     * @param orgUpdateDto 组织机构更新数据封装类
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addOrganization(HttpServletRequest request, @RequestBody @Valid OrgUpdateDto orgUpdateDto){
        boolean res = orgService.addOrganization(orgUpdateDto, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改组织机构节点信息
     * @param request http请求对象
     * @param orgUpdateDto 组织机构更新信息封装类
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyOrganization(HttpServletRequest request, @RequestBody @Valid OrgUpdateDto orgUpdateDto){
        if (orgUpdateDto.getId() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = orgService.modifyOrganization(orgUpdateDto, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 根据ID删除组织机构
     * @param request http请求对象
     * @param id 阻止机构节点ID
     * @return 请求响应结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeOrganization(HttpServletRequest request, Long id){
        boolean res = false;
        try {
            res = orgService.removeOrganization(id, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
